package com.thunder.soft.common.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.thunder.soft.common.listener.IRecyclerViewItemClickListener;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseAdapter<T, VDB extends ViewDataBinding> extends
        RecyclerView.Adapter<BaseViewHolder<VDB>> {

    private static final String TAG = "BaseAdapter";
    // 数据源
    private List<T> list;
    private Context context;
    private IRecyclerViewItemClickListener<T> itemClickListener;

    @NonNull
    @Override
    public BaseViewHolder<VDB> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        this.context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);
        return new BaseViewHolder<>(DataBindingUtil.inflate(inflater, layoutId(viewType), parent, false));
    }

    @LayoutRes
    protected abstract int layoutId(int viewType);

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder<VDB> holder, int position) {
        onBindHolder(DataBindingUtil.getBinding(holder.itemView), holder, position, getData(position), null);
    }

    /**
     * 逻辑调用函数
     *
     * @param binding  布局文件
     * @param holder   holder
     * @param position 下标
     * @param data     当前数据
     * @param payloads 局部更新
     */
    protected void onBindHolder(VDB binding, @NonNull BaseViewHolder<VDB> holder, int position, @NonNull T data, List<Object> payloads) {

        holder.itemView.setOnClickListener(v -> {
            if (itemClickListener != null) {
                itemClickListener.onClickListener(position, getData(position));
            }
        });
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder<VDB> holder, int position, @NonNull List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        } else {
            onBindHolder(DataBindingUtil.getBinding(holder.itemView), holder, position, getData(position), payloads);
        }
    }

    /**
     * 数据源
     *
     * @return 返回数据源
     */
    public List<T> getList() {
        return list;
    }

    /**
     * 根据position获取数据
     *
     * @param position 下标
     * @return 返回当前数据
     */
    public T getData(int position) {
        try {
            return list.get(position);
        } catch (Exception e) {
            Log.e(TAG, "下标越界异常: ", e);
            return null;
        }
    }

    /**
     * 获取上下文
     * @return 上下文
     */
    public Context getContext() {
        return context;
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    /**
     * 添加list数据
     * @param list 数据源
     */
    @SuppressLint("NotifyDataSetChanged")
    public void addList(List<T> list) {
        if (this.list == null) {
            this.list = new ArrayList<>();
        }
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    /**
     * 添加一条数据
     * @param data 数据
     */
    @SuppressLint("NotifyDataSetChanged")
    public void addData(T data) {
        if (this.list == null) {
            this.list = new ArrayList<>();
        }
        this.list.add(data);
        notifyDataSetChanged();
    }

    /**
     * 添加list数据 更新最新list.size数据
     * @param list 数据
     */
    public void addOldAndNew(List<T> list) {
        if (this.list == null) {
            this.list = new ArrayList<>();
        }
        int old = this.list.size();
        this.list.addAll(list);
        notifyItemRangeChanged(old, this.list.size());
    }

    /**
     * 清空数据并重新添加
     * @param list 数据源
     */
    @SuppressLint("NotifyDataSetChanged")
    public void addListClear(List<T> list) {
        if (this.list == null) {
            this.list = new ArrayList<>();
        } else {
            this.list.clear();
        }
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    /**
     * 点击回调函数
     * @param itemClickListener 回调方法
     */
    public void setItemClickListener(IRecyclerViewItemClickListener<T> itemClickListener) {
        this.itemClickListener = itemClickListener;
    }
}
